In [1]:
%matplotlib inline
from ggplot import *
import pandas as pd

Plotting two variables as lines on the same graph

So you've got 2 variables and you want to plot them on the same chart? How do you do it in ggplot? Well good news is it's super easy to do with ggplot!

We're going to use a subset of the meat dataset for this example. We're going to use pandas to switch our data from "wide" to "long" format.


In [2]:
meat_subset = meat[['date', 'beef', 'pork']]
df = pd.melt(meat_subset, id_vars=['date'])
df.head()


Out[2]:
date variable value
0 1944-01-01 beef 751.0
1 1944-02-01 beef 713.0
2 1944-03-01 beef 741.0
3 1944-04-01 beef 650.0
4 1944-05-01 beef 681.0

Now we'll setup our aesthetics so date is the x-axis value, variable is the color of each line and value is the y-axis value.


In [3]:
ggplot(df, aes(x='date', y='value', color='variable')) + geom_line()


Out[3]:
<ggplot: (274447253)>

In [ ]: